<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>fontsizemonitor.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_dom_fontsizemonitor.js.html">fontsizemonitor.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2005 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A class that can be used to listen to font size changes.
<a name="line17"></a> * @author arv@google.com (Erik Arvidsson)
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.dom.FontSizeMonitor&#39;);
<a name="line21"></a>goog.provide(&#39;goog.dom.FontSizeMonitor.EventType&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.dom&#39;);
<a name="line24"></a>goog.require(&#39;goog.events&#39;);
<a name="line25"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line26"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line27"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>// TODO(arv): Move this to goog.events instead.
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * This class can be used to monitor changes in font size.  Instances will
<a name="line36"></a> * dispatch a {@code goog.dom.FontSizeMonitor.EventType.CHANGE} event.
<a name="line37"></a> * Example usage:
<a name="line38"></a> * &lt;pre&gt;
<a name="line39"></a> * var fms = new goog.dom.FontSizeMonitor();
<a name="line40"></a> * goog.events.listen(fms, goog.dom.FontSizeMonitor.EventType.CHANGE,
<a name="line41"></a> *     function(e) {
<a name="line42"></a> *       alert(&#39;Font size was changed&#39;);
<a name="line43"></a> *     });
<a name="line44"></a> * &lt;/pre&gt;
<a name="line45"></a> * @param {goog.dom.DomHelper=} opt_domHelper DOM helper object that is used to
<a name="line46"></a> *     determine where to insert the DOM nodes used to determine when the font
<a name="line47"></a> *     size changes.
<a name="line48"></a> * @constructor
<a name="line49"></a> * @extends {goog.events.EventTarget}
<a name="line50"></a> * @final
<a name="line51"></a> */
<a name="line52"></a>goog.dom.FontSizeMonitor = function(opt_domHelper) {
<a name="line53"></a>  goog.events.EventTarget.call(this);
<a name="line54"></a>
<a name="line55"></a>  var dom = opt_domHelper || goog.dom.getDomHelper();
<a name="line56"></a>
<a name="line57"></a>  /**
<a name="line58"></a>   * Offscreen iframe which we use to detect resize events.
<a name="line59"></a>   * @type {Element}
<a name="line60"></a>   * @private
<a name="line61"></a>   */
<a name="line62"></a>  this.sizeElement_ = dom.createDom(
<a name="line63"></a>      // The size of the iframe is expressed in em, which are font size relative
<a name="line64"></a>      // which will cause the iframe to be resized when the font size changes.
<a name="line65"></a>      // The actual values are not relevant as long as we can ensure that the
<a name="line66"></a>      // iframe has a non zero size and is completely off screen.
<a name="line67"></a>      goog.userAgent.IE ? &#39;div&#39; : &#39;iframe&#39;, {
<a name="line68"></a>        &#39;style&#39;: &#39;position:absolute;width:9em;height:9em;top:-99em&#39;,
<a name="line69"></a>        &#39;tabIndex&#39;: -1,
<a name="line70"></a>        &#39;aria-hidden&#39;: &#39;true&#39;
<a name="line71"></a>      });
<a name="line72"></a>  var p = dom.getDocument().body;
<a name="line73"></a>  p.insertBefore(this.sizeElement_, p.firstChild);
<a name="line74"></a>
<a name="line75"></a>  /**
<a name="line76"></a>   * The object that we listen to resize events on.
<a name="line77"></a>   * @type {Element|Window}
<a name="line78"></a>   * @private
<a name="line79"></a>   */
<a name="line80"></a>  var resizeTarget = this.resizeTarget_ =
<a name="line81"></a>      goog.userAgent.IE ? this.sizeElement_ :
<a name="line82"></a>          goog.dom.getFrameContentWindow(
<a name="line83"></a>              /** @type {HTMLIFrameElement} */ (this.sizeElement_));
<a name="line84"></a>
<a name="line85"></a>  // We need to open and close the document to get Firefox 2 to work.  We must
<a name="line86"></a>  // not do this for IE in case we are using HTTPS since accessing the document
<a name="line87"></a>  // on an about:blank iframe in IE using HTTPS raises a Permission Denied
<a name="line88"></a>  // error.
<a name="line89"></a>  if (goog.userAgent.GECKO) {
<a name="line90"></a>    var doc = resizeTarget.document;
<a name="line91"></a>    doc.open();
<a name="line92"></a>    doc.close();
<a name="line93"></a>  }
<a name="line94"></a>
<a name="line95"></a>  // Listen to resize event on the window inside the iframe.
<a name="line96"></a>  goog.events.listen(resizeTarget, goog.events.EventType.RESIZE,
<a name="line97"></a>                     this.handleResize_, false, this);
<a name="line98"></a>
<a name="line99"></a>  /**
<a name="line100"></a>   * Last measured width of the iframe element.
<a name="line101"></a>   * @type {number}
<a name="line102"></a>   * @private
<a name="line103"></a>   */
<a name="line104"></a>  this.lastWidth_ = this.sizeElement_.offsetWidth;
<a name="line105"></a>};
<a name="line106"></a>goog.inherits(goog.dom.FontSizeMonitor, goog.events.EventTarget);
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * The event types that the FontSizeMonitor fires.
<a name="line111"></a> * @enum {string}
<a name="line112"></a> */
<a name="line113"></a>goog.dom.FontSizeMonitor.EventType = {
<a name="line114"></a>  // TODO(arv): Change value to &#39;change&#39; after updating the callers.
<a name="line115"></a>  CHANGE: &#39;fontsizechange&#39;
<a name="line116"></a>};
<a name="line117"></a>
<a name="line118"></a>
<a name="line119"></a>/**
<a name="line120"></a> * Constant for the change event.
<a name="line121"></a> * @type {string}
<a name="line122"></a> * @deprecated Use {@code goog.dom.FontSizeMonitor.EventType.CHANGE} instead.
<a name="line123"></a> */
<a name="line124"></a>goog.dom.FontSizeMonitor.CHANGE_EVENT =
<a name="line125"></a>    goog.dom.FontSizeMonitor.EventType.CHANGE;
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/** @override */
<a name="line129"></a>goog.dom.FontSizeMonitor.prototype.disposeInternal = function() {
<a name="line130"></a>  goog.dom.FontSizeMonitor.superClass_.disposeInternal.call(this);
<a name="line131"></a>
<a name="line132"></a>  goog.events.unlisten(this.resizeTarget_, goog.events.EventType.RESIZE,
<a name="line133"></a>                       this.handleResize_, false, this);
<a name="line134"></a>  this.resizeTarget_ = null;
<a name="line135"></a>
<a name="line136"></a>  // Firefox 2 crashes if the iframe is removed during the unload phase.
<a name="line137"></a>  if (!goog.userAgent.GECKO ||
<a name="line138"></a>      goog.userAgent.isVersionOrHigher(&#39;1.9&#39;)) {
<a name="line139"></a>    goog.dom.removeNode(this.sizeElement_);
<a name="line140"></a>  }
<a name="line141"></a>  delete this.sizeElement_;
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * Handles the onresize event of the iframe and dispatches a change event in
<a name="line147"></a> * case its size really changed.
<a name="line148"></a> * @param {goog.events.BrowserEvent} e The event object.
<a name="line149"></a> * @private
<a name="line150"></a> */
<a name="line151"></a>goog.dom.FontSizeMonitor.prototype.handleResize_ = function(e) {
<a name="line152"></a>  // Only dispatch the event if the size really changed.  Some newer browsers do
<a name="line153"></a>  // not really change the font-size,  instead they zoom the whole page.  This
<a name="line154"></a>  // does trigger window resize events on the iframe but the logical pixel size
<a name="line155"></a>  // remains the same (the device pixel size changes but that is irrelevant).
<a name="line156"></a>  var currentWidth = this.sizeElement_.offsetWidth;
<a name="line157"></a>  if (this.lastWidth_ != currentWidth) {
<a name="line158"></a>    this.lastWidth_ = currentWidth;
<a name="line159"></a>    this.dispatchEvent(goog.dom.FontSizeMonitor.EventType.CHANGE);
<a name="line160"></a>  }
<a name="line161"></a>};
</pre>


</body>
</html>
